package uk.co.droidinactu.inlibrislibertas.db;

import uk.co.droidinactu.common.db.AbstractDatabaseHelper;
import uk.co.droidinactu.common.db.AbstractDbConstants;
import uk.co.droidinactu.inlibrislibertas.model.Author;
import uk.co.droidinactu.inlibrislibertas.model.AuthorEbookLink;
import uk.co.droidinactu.inlibrislibertas.model.DbMetadata;
import uk.co.droidinactu.inlibrislibertas.model.EBook;
import uk.co.droidinactu.inlibrislibertas.model.EBookMetaData;
import uk.co.droidinactu.inlibrislibertas.model.RecentEBook;
import uk.co.droidinactu.inlibrislibertas.model.Tag;
import uk.co.droidinactu.inlibrislibertas.model.TagEbookLink;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

/**
 * 
 * @author aspela
 * 
 */
public class DatabaseHelper extends AbstractDatabaseHelper {

    private boolean createSampleDataOnStartup = false;
    private boolean firstTimeThrough = true;

    public DatabaseHelper(final Context context, final AbstractDbConstants dbConstants, final boolean createSamples) {
        super(context, dbConstants);
        createSampleDataOnStartup = createSamples;
    }

    private void createTables(final SQLiteDatabase db) {
        db.execSQL(new EBook().getSqlCreate());
        db.execSQL(new Author().getSqlCreate());
        db.execSQL(new AuthorEbookLink().getSqlCreate());
        db.execSQL(new Tag().getSqlCreate());
        db.execSQL(new TagEbookLink().getSqlCreate());
        db.execSQL(new DbMetadata().getSqlCreate());
        db.execSQL(new EBookMetaData().getSqlCreate());
        db.execSQL(new RecentEBook().getSqlCreate());
    }

    private void doUpgradeFrom001(final SQLiteDatabase db) {
        db.execSQL(new EBook().getSqlUpdateFromV001());
        db.execSQL(new Author().getSqlUpdateFromV001());
        db.execSQL(new AuthorEbookLink().getSqlUpdateFromV001());
        db.execSQL(new Tag().getSqlUpdateFromV001());
        db.execSQL(new TagEbookLink().getSqlUpdateFromV001());
        db.execSQL(new DbMetadata().getSqlUpdateFromV001());
        db.execSQL(new EBookMetaData().getSqlUpdateFromV001());
        db.execSQL(new RecentEBook().getSqlUpdateFromV001());
    }

    private void doUpgradeFrom002(final SQLiteDatabase db) {
        db.execSQL(new EBook().getSqlUpdateFromV002());
        db.execSQL(new Author().getSqlUpdateFromV002());
        db.execSQL(new AuthorEbookLink().getSqlUpdateFromV002());
        db.execSQL(new Tag().getSqlUpdateFromV002());
        db.execSQL(new TagEbookLink().getSqlUpdateFromV002());
        db.execSQL(new DbMetadata().getSqlUpdateFromV002());
        db.execSQL(new EBookMetaData().getSqlUpdateFromV002());
        db.execSQL(new RecentEBook().getSqlUpdateFromV002());
    }

    private void dropTables(final SQLiteDatabase db) {
        db.execSQL("DROP TABLE IF EXISTS " + new EBook().getTableName());
        db.execSQL("DROP TABLE IF EXISTS " + new Author().getTableName());
        db.execSQL("DROP TABLE IF EXISTS " + new AuthorEbookLink().getTableName());
        db.execSQL("DROP TABLE IF EXISTS " + new Tag().getTableName());
        db.execSQL("DROP TABLE IF EXISTS " + new TagEbookLink().getTableName());
        db.execSQL("DROP TABLE IF EXISTS " + new DbMetadata().getTableName());
        db.execSQL("DROP TABLE IF EXISTS " + new EBookMetaData().getTableName());
        db.execSQL("DROP TABLE IF EXISTS " + new RecentEBook().getTableName());
    }

    @Override
    public void onCreate(final SQLiteDatabase db) {
        super.onCreate(db);
        createTables(db);
    }

    @Override
    public void onOpen(final SQLiteDatabase db) {
        super.onOpen(db);
        if (createSampleDataOnStartup && firstTimeThrough) {
            dropTables(db);
            createTables(db);
            firstTimeThrough = false;
        }
    }

    @Override
    public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
        super.onUpgrade(db, oldVersion, newVersion);
        switch (oldVersion) {
        case (1):
            doUpgradeFrom001(db);
        case (2):
            doUpgradeFrom002(db);
        }
        dropTables(db);
        onCreate(db);
    }

}
